##############################################################
####State Capacity, Insurgency, and Civil War - SP Weibull####
##############################################################
library(MASS) 
library(pscl)
library(foreign)
library(Hmisc)
library(rgdal)
library(RArcInfo)
library(stargazer)
library(mvtnorm)
library(Zelig)
library(readstata13)
library(pROC)
library(robust)
library(cvTools)
library(boot)
library(maps)
library(mapdata)
library(doBy)
library(raster)
library(survival)
library(spduration)
library(DAMisc)
library(plyr)
library(DataCombine)
library(plm)
library(AER)
library(lmtest)

########################
###Create The Dataset###
########################
###Importing the main data
#set working directory to location of dta file
setwd("~/Google Drive/NSLC/Final Dofiles/")
#Import the data
main.data <- read.dta("ntlgrid.dta")

#Create duration dataset
#Omit NAs
main.data.omit <- main.data[!is.na(main.data$onset),]
#Create duration data
dur.dat <- add_duration(main.data.omit, "onset", "gid", "year", freq="year", ongoing="TRUE") 

##########################
###CW Onset replication###
##########################
###Model 1: Civil War
#Baseline SP
CW1.sp.dur.small <-  spdur(duration ~ lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                             logross_oil_prod + nwstate + instability + lagp_polity2 + ethfrac + relfrac +
                             logcellarea + logttime, 
                           atrisk ~ lagcivconflagtemp + loglagpop + logttime + logcellarea, 
                           data = dur.dat)
summary(CW1.sp.dur.small)
AIC(CW1.sp.dur.small)
#Full SP
CW1.sp.dur.large <- spdur(duration ~ lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                            logross_oil_prod + nwstate + instability + lagp_polity2 + ethfrac + relfrac +
                            logcellarea + logttime, 
                          atrisk ~ lagcivconflagtemp + loglagppp + loglagpop +  
                            nwstate + instability + lagp_polity2 + ethfrac +
                            logcellarea + logttime, 
                          data = dur.dat)
summary(CW1.sp.dur.large)
AIC(CW1.sp.dur.large)

###Model 2: Ethnic War
##Create new dataset
main.data.eth <- main.data[ which(main.data$second > 0.04999),]
#Omit NAs
main.data.eth.omit <- main.data.eth[!is.na(main.data.eth$onset),]
#Create duration data
dur.dat.eth <- add_duration(main.data.eth.omit, "onset", "gid", "year", freq="year", ongoing="TRUE") 

#Baseline SP
EW.sp.dur.small <-  spdur(duration ~lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                            logross_oil_prod + nwstate + instability + lagp_polity2 + ethfrac + relfrac +
                            logcellarea + logttime, 
                          atrisk ~ lagcivconflagtemp + loglagpop + logttime + logcellarea, 
                          data = dur.dat.eth)
summary(EW.sp.dur.small)
AIC(EW.sp.dur.small)

#Full SP
EW.sp.dur.large <- spdur(duration ~ lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                           logross_oil_prod + nwstate + instability + lagp_polity2 + ethfrac + relfrac +
                           logcellarea + logttime, 
                         atrisk ~ lagcivconflagtemp + loglagppp + loglagpop +  
                           nwstate + instability + lagp_polity2 + ethfrac +
                           logcellarea + logttime, 
                         data = dur.dat.eth)
summary(EW.sp.dur.large)
AIC(EW.sp.dur.large)

###Model 3: Civil War
#Baseline SP
CW2.sp.dur.small <-  spdur(duration ~ lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                             logross_oil_prod + nwstate + instability + ethfrac + relfrac +
                             laganocracy + lagpolbin + logcellarea + logttime, 
                           atrisk ~ lagcivconflagtemp + loglagpop + logttime + logcellarea, 
                           data = dur.dat)
summary(CW2.sp.dur.small)
AIC(CW2.sp.dur.small)
#Full SP
CW2.sp.dur.large <- spdur(duration ~lnNL_sum + lagcivconflagtemp + loglagppp + loglagpop + mnt1 + logbdist1 +  
                            logross_oil_prod + nwstate + instability + ethfrac + relfrac +
                            laganocracy + lagpolbin + logcellarea + logttime, 
                          atrisk ~ lagcivconflagtemp + loglagppp + loglagpop +  
                            nwstate + instability + laganocracy + lagpolbin + ethfrac +
                            logcellarea + logttime, 
                          data = dur.dat)
summary(CW2.sp.dur.large)
AIC(CW2.sp.dur.large)